import re
import requests
from bs4 import BeautifulSoup
import json

response = requests.get('https://ncov.dxy.cn/ncovh5/view/pneumonia')
home_page = response.content.decode()
# print(home_page)
soup = BeautifulSoup(home_page, 'lxml')
script = soup.find(id='getListByCountryTypeService2true')
text = script.text
print(text)
json_str = re.findall(r'\[.+\]', text)[0]
print(json_str)
# 转换为python
python = json.loads(json_str)
# 存文件
with open('疫情数据/最新各国疫情数据.json','w',encoding='utf8') as fp:
    json.dump(python,fp,ensure_ascii=False)

print('爬取完成')